﻿
@{
    ViewBag.Title = "库存单价";
    Layout = "~/Views/Jxc/_Layout.cshtml";
}

<h2>@ViewBag.Title</h2>


@Styles.Render("~/Content/dataTableCss")
@Scripts.Render("~/bundles/dataTableJs")
<script src="~/WebExtend/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
    $.ajaxSettings.async = false;//必须要同步读取商品资料，否则会导致合计之类的运算错误

    function GetProduct(code) {
        LoadProduct(code);
    }

    function LoadProduct(code) { 
        $.get("/Basic/GetProductJson", {
            code: code 
        },
            function (data) {
            var id = data["Id"];
            if (id == -1) {
                alert("找不到指定商品");
            }
            else {
                var pri = data["StorePrice"];

                var tr = '<tr><td>' + data["Code"] + '</td>';
                tr += ("<td>" + data["Barcode"] + "</td>");
                tr += ("<td>" + data["Name"] + "</td>");
                tr += ('<td>' + data["Brand"]["Name"] + "</td>");
                tr += ('<td>' + data["Model"]["Name"] + "</td>");
                tr += ("<td>" + data["Unit"]["Name"] + "</td>");
                tr += ('<td>' + data["StorePrice"] + '</td>');
                tr += ('<td><input type="number" min="0" step="0.01" value="' + data["StorePrice"] + '" /></td>');
                tr += ('<td> <input type="hidden" value=' + data["Id"] + ' /><button type="button" class="btn btn-danger">更新</button></td></tr>');
                AddTableTr(tr);
                $("#InputCode").val("");
            }
        });
    }

    function AddTableTr(trHtml) {
        var $tr = $("#tableDetail tr:first");
        if ($tr.size() == 0) {
            alert("指定的table id或行数不存在！");
            return;
        }
        $tr.after(trHtml);
    }

    function RemoveTableTr(rowIndex) {
        var $tr = $("#tableDetail tr").eq(rowIndex);
        if ($tr.size() == 0) {
            alert("指定的table id或行数不存在！");
            return;
        }
        $tr.remove();
    }

    function InitDataTable() {
        $("table").DataTable({
            "paging": false,
            "ordering": false,
            "info": false,
            "searching": false
        });
    }

    $(document).ready(function () {
        InitDataTable();
        $("#tableDetail tbody").empty();

        $("#LoadProduct").click(function () {
            var pForm = window.open("/Basic/GetProductCategoryList", null, " height=480,width=640,Left=300px,Top=20px, menubar=no,titlebar=no,scrollbar=yes,toolbar=no, status=no,location=no");
            if (pForm) {
                window.pForm.focus();
            }
        });

        $("#InputCode").keypress(function (e) {
            if (e.keyCode == 13) {
                LoadProduct($("#InputCode").val());
            }
        });

        $("body").on("click", ".btn-danger", function () {
            var tr = $(this).parent().parent();
            var all = Number(tr.children("td:eq(6)").text());
            var ok = Number(tr.children("td:eq(7)").children().val());
            if (ok < 0) {
                alert("库存单价错误");
                return;
            } 
            tr.children("td:eq(6)").text(ok);
            $.post("/Jxc/StorePriceConfirm", { id: $(this).prev().val(), price: ok });
        });

    });
</script>
<hr />
<div class="form-inline">
    输入商品编码/条形码:
    <div class="form-group">
        <input type="text" id="InputCode" class="form-control text-box single-line" />
    </div>
    <input type="button" value="加载" id="LoadProduct" class="btn btn-info" />
</div>

<div id="tableDetail">
    <table class="display">
        <thead>
            <tr>
                <th>编码</th>
                <th>条形码</th>
                <th>名称</th>
                <th>品牌</th>
                <th>型号</th>
                <th>单位</th>
                <th>库存价</th>
                <th>新库存单价</th>
                <th></th>
            </tr>
        </thead>
        <tbody></tbody>
        <tfoot> </tfoot>
    </table>
</div>

